package com.yulan.bankback.service.admin.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yulan.bankback.entity.dto.admin.ViolationAgesDto;
import com.yulan.bankback.entity.dto.admin.ViolationRegionDto;
import com.yulan.bankback.entity.model.admin.EverydayVUCount;
import com.yulan.bankback.entity.model.admin.ViolationUser;
import com.yulan.bankback.entity.vo.ViolationUserVo;
import com.yulan.bankback.mapper.admin.EverydayVUCountMapper;
import com.yulan.bankback.mapper.admin.ViolationUserMapper;
import com.yulan.bankback.service.admin.AdminService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class AdminServiceImpl implements AdminService {

    @Resource
    private ViolationUserMapper violationUserMapper;

    @Resource
    private EverydayVUCountMapper everydayVUCountMapper;



    private static final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    private static final SimpleDateFormat formatter2 = new SimpleDateFormat("yyyy-MM-dd");

    private static final SimpleDateFormat formatter3 = new SimpleDateFormat("MM-dd");


    @Override
    public ViolationUserVo getViolationUserVo(String interval) {
        ViolationUserVo vo = new ViolationUserVo();
        List<ViolationUser> violationUsers = violationUserMapper.getViolationUserInterval(interval);
        violationUsers.forEach(item -> item.setViewViolationTime(formatter.format(item.getViolationTime())));


        List<EverydayVUCount> everydayVUCounts = everydayVUCountMapper.getDataInterval(interval);
        everydayVUCounts.forEach(item -> item.setViewDate(formatter3.format(item.getTime())));

        //折线图日期
        List<String> violationFreqDate = everydayVUCounts.stream().
                map(EverydayVUCount::getViewDate)
                .collect(Collectors.toList());

        while (violationFreqDate.size() > 10){
            violationFreqDate = this.changeListSize(violationFreqDate, 4);
        }


        //折线图数据
        List<Integer> violationFreqData = everydayVUCounts.stream()
                .map(EverydayVUCount::getCount)
                .collect(Collectors.toList());

        while (violationFreqData.size() > 10){
            violationFreqData = this.changeListSize(violationFreqData, 4);
        }

        vo.setViolationFreqDate(violationFreqDate);
        vo.setViolationFreqData(violationFreqData);

        //地区分布
        List<ViolationRegionDto> violationRegionDtos = violationUserMapper.getRegionGroup(interval, 10);

        vo.setViolationRegions(violationRegionDtos);

        List<ViolationAgesDto> violationAgesDtos = new ArrayList<>();

        HashMap<Long, Integer> ageMap = new HashMap<>();


        Date curDate = new Date();
        violationUsers.forEach(item ->{
            Long year = DateUtil.betweenYear(item.getBornDate(), curDate, false);
            ageMap.put(year, ageMap.get(year) == null ? 1 :  ageMap.get(year) + 1);
        });


        for(Long year : ageMap.keySet()){
            violationAgesDtos.add(new ViolationAgesDto(year , ageMap.get(year)));
        }

        if(violationAgesDtos.size() > 10){
            violationAgesDtos = violationAgesDtos.subList(0, 10);
        }

        vo.setViolationAges(violationAgesDtos);

        return vo;
    }

    @Override
    public List<ViolationUser> getByConditions(String start, String end, String type,
                                              String username, String uid) {
        QueryWrapper<ViolationUser> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(start)) {
            queryWrapper.gt("violation_time", DateUtil.parseDate(start));
        }
        if(StringUtils.isNotBlank(end)) {
            queryWrapper.lt("violation_time", DateUtil.parseDate(end));
        }
        if(StringUtils.isNotBlank(type)) queryWrapper.eq("violation_type", type);
        if(StringUtils.isNotBlank(username)) queryWrapper.eq("username", username);
        if(StringUtils.isNotBlank(uid)) queryWrapper.eq("uid", uid);
        List<ViolationUser> res = violationUserMapper.selectList(queryWrapper);
        res.forEach(item -> item.setViewViolationTime(formatter.format(item.getViolationTime())));
        return res;
    }

    private <T> List<T> changeListSize(List<T> list, int mod){
        T first = list.get(0);
        T last = list.get(list.size() - 1);

        List<T> res = new ArrayList<>();
        res.add(first);
        for(int i = 1; i < list.size() - 1; i++){
            if(i % mod != 0)
                res.add(list.get(i));
        }
        res.add(last);
        return res;
    }

}
